package com.longsunhd.yum.buffalo.util.generator;

import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classichu.lineseditview.LinesEditView;
import com.longsunhd.yum.buffalo.R;
import com.longsunhd.yum.buffalo.base.ControlsActivity;
import com.longsunhd.yum.buffalo.bean.DragListBean;
import com.longsunhd.yum.buffalo.bean.FieldFormationBean;
import com.longsunhd.yum.buffalo.bean.GetMedicationFieldBean;
import com.longsunhd.yum.buffalo.network.action.MedicateApiUtils;
import com.longsunhd.yum.buffalo.util.DataPickerUtil;
import com.longsunhd.yum.buffalo.util.ViewUtils;
import com.longsunhd.yum.buffalo.widget.nicespinner.ISpinnerName;
import com.longsunhd.yum.buffalo.widget.nicespinner.MaterialSpinner;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;


/**
 * 作者：黄宣淇
 * 时间：2021/5/12 0012
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public class UIGenerator {
    private static final String hidden="hidden";
    private static final String select="select";
    private static final String text="text";
    private static final String radio="radio";
    private static final String datetime="datetime";
    private static final String digit="digit";
    private static final String textarea="textarea";
    private static final String number="number";
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void GenerateEditDetailUi(ControlsActivity controlsActivity, String token,
                                            FieldFormationBean formBean,
                                            ViewGroup mll_content){
       
        if (formBean.getType().equals(hidden)) {
            //隐藏不需要显示为控件

        } else if (formBean.getType().equals(select)) {
            //下拉
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_spinner);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            MaterialSpinner mSpinner = view.findViewById(R.id.mSpinner);
            MedicateApiUtils.newInstance().getDragList(token, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    DragListBean dragListBean = (DragListBean) msg.obj;
                    List<DragListBean.DataBean> listBeanData = dragListBean.getData();
                    HashMap<String, String> spinnerHashMap = new HashMap<>();

                    if (listBeanData != null&&listBeanData.size()>0) {
                        mSpinner.setItems(listBeanData);
                        mSpinner.setSelectedIndex(0);
                        spinnerHashMap.put(formBean.getField(), listBeanData.get(0).getId()+"");
                        mSpinner.setTag(spinnerHashMap);
                        mSpinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(MaterialSpinner view, int position, long id, ISpinnerName item) {
                                String dragID = dragListBean.getData().get(position).getId() + "";

                                spinnerHashMap.put(formBean.getField(), dragID);
                                mSpinner.setTag(spinnerHashMap);
                            }
                        });
                    
                        controlsActivity.getSpinnerList().add(mSpinner);
                    }
                }
            });

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            mSpinner.setTag(formBean.getField());
        } else if (formBean.getType().equals(text)) {
            //普通文本框
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_input);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            EditText mEtValue = view.findViewById(R.id.mEtValue);

            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            Optional<Object> value = Optional.ofNullable(formBean.getValue());
            Object valueStr = value.orElse("");
            mEtValue.setText(valueStr + "");
            int height = controlsActivity.getResources().getDimensionPixelSize(R.dimen.dp_50);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , height);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            mEtValue.setTag(formBean.getField());
            controlsActivity.getEditTextList().add(mEtValue);
        }else if(formBean.getType().equals(digit)){
            /*数据类型*/

            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_digit);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            EditText mEtValue = view.findViewById(R.id.mEtValue);

            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            Optional<Object> value = Optional.ofNullable(formBean.getValue());
            Object valueStr = value.orElse("");
            mEtValue.setText(valueStr + "");
            int height = controlsActivity.getResources().getDimensionPixelSize(R.dimen.dp_50);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , height);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            mEtValue.setTag(formBean.getField());
            controlsActivity.getEditTextList().add(mEtValue);
        }else if(formBean.getType().equals(number)){
            /*整数类型*/
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_number);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            EditText mEtValue = view.findViewById(R.id.mEtValue);

            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            Optional<Object> value = Optional.ofNullable(formBean.getValue());
            Object valueStr = value.orElse("");
            mEtValue.setText(valueStr + "");
            int height = controlsActivity.getResources().getDimensionPixelSize(R.dimen.dp_50);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , height);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            mEtValue.setTag(formBean.getField());
            controlsActivity.getEditTextList().add(mEtValue);
        } else if (formBean.getType().equals(radio)) {
            //单选框
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_radio);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            RadioGroup radioGroup = view.findViewById(R.id.mRadioGroup);/*
                        RadioButton radio_button1=view.findViewById(R.id.radio_button1);
                        RadioButton radio_button2=view.findViewById(R.id.radio_button2);*/
            RadioButton radio_button3 = view.findViewById(R.id.radio_button3);
            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            List<GetMedicationFieldBean.DataBean.FormBean.OptionBean> optionBeanList = formBean.getOption();
            if (optionBeanList != null) {
                if (optionBeanList.size() == 2) {
                    radio_button3.setVisibility(View.GONE);
                }
                for (int i = 0; i < optionBeanList.size(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    radioButton.setText(optionBeanList.get(i).getName());
                    radioButton.setTag(optionBeanList.get(i).getValue() + "");//用tag来存value
                }
            }
            int height = controlsActivity.getResources().getDimensionPixelSize(R.dimen.dp_50);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , height);
            radioGroup.setLayoutParams(layoutParams);
            mll_content.addView(view);
            radioGroup.setTag(formBean.getField());
             controlsActivity.getRadioGroups().add(radioGroup);
        } else if (formBean.getType().equals(datetime)) {
            //日期控件
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_date_picker);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            TextView mTvValue = view.findViewById(R.id.mTvValue);
            Optional<Object> value = Optional.ofNullable(formBean.getValue());
            Object valueStr = value.orElse("");
            mTvValue.setText(valueStr + "");
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DataPickerUtil.showDataPicker(controlsActivity, mTvValue);
                }
            });
            int height = controlsActivity.getResources().getDimensionPixelSize(R.dimen.dp_50);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, height);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            mTvValue.setTag(formBean.getField());
             controlsActivity.getTextViewList().add(mTvValue);
        } else if (formBean.getType().equals(textarea)) {
            //文本域
            View view = ViewUtils.getLayoutView(controlsActivity, R.layout.layout_muti_text);
            TextView mTvkey = view.findViewById(R.id.mTvkey);
            Optional<String> key = Optional.ofNullable(formBean.getName());
            String keyStr = key.orElse("");
            mTvkey.setText(keyStr + "");
            LinesEditView linesEditView = view.findViewById(R.id.mLinesEtValue);
            Optional<Object> value = Optional.ofNullable(formBean.getValue());
            String valueStr = key.orElse("");
            linesEditView.setContentText(valueStr + "");

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(layoutParams);
            mll_content.addView(view);
            linesEditView.setTag(formBean.getField());
             controlsActivity.getLinesEditViewList().add(linesEditView);
        }
        Optional<Object> value = Optional.ofNullable(formBean.getValue());
        Object o = value.orElse("");
         controlsActivity.getHashMap().put(formBean.getField(), o + "");
    }
    public  static void SumbitAction(ControlsActivity controlsActivity){
        for (EditText editText : controlsActivity.getEditTextList()) {
            controlsActivity.getHashMap().put((String) editText.getTag(), editText.getText().toString());
        }
        /*单选框*/
        for (RadioGroup radioGroup : controlsActivity.getRadioGroups()) {
            for (int i = 0; i < radioGroup.getChildCount(); i++) {
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                if (radioButton.isChecked()) {
                    controlsActivity.getHashMap().put((String) radioGroup.getTag(), (String) radioButton.getTag());
                }
            }
        }
        /*日期*/
        for (TextView textView : controlsActivity.getTextViewList()) {
            controlsActivity.getHashMap().put((String) textView.getTag(), textView.getText().toString());
        }
        /*下拉列表*/
        for (MaterialSpinner spinner : controlsActivity.getSpinnerList()) {
            HashMap<String, String> stringStringHashMap = (HashMap<String, String>) spinner.getTag();

            Iterator i = stringStringHashMap.keySet().iterator();

            Iterator i1 = stringStringHashMap.values().iterator();
            while (i.hasNext() && i1.hasNext()) {
                controlsActivity.getHashMap().put(i.next() + "", i1.next() + "");
                break;
            }
        }
        /*文本域*/
        for (LinesEditView linesEditView : controlsActivity.getLinesEditViewList()) {
            controlsActivity.getHashMap().put((String) linesEditView.getTag(), linesEditView.getContentText());
        }
    }
    public static void GeneratorBuffaloDetailUI(Activity mActivity,int layoutId,ViewGroup root, String keyStr,String valueStr){
        if(TextUtils.isEmpty(valueStr))return;
        View view = ViewUtils.getLayoutView(mActivity,layoutId);
        TextView mTvkey = view.findViewById(R.id.mTvkey);
        TextView mTvValue = view.findViewById(R.id.mTvValue);
        mTvkey.setText(keyStr);
        mTvValue.setText(valueStr);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        root.addView(view);
    }
}
